package net.randomice.emf.observables.demo.extlibrary.items.observables;

import java.util.List;
import java.util.Set;
import net.randomice.emf.observables.demo.extlibrary.items.Book;
import net.randomice.emf.observables.demo.extlibrary.items.Item;
import net.randomice.emf.observables.demo.extlibrary.items.ItemsFactory;
import net.randomice.emf.observables.demo.extlibrary.items.ItemsPackage;
import net.randomice.emf.observables.demo.extlibrary.persons.Writer;
import net.randomice.emf.observables.demo.extlibrary.persons.observables.WriterOM;
import net.randomice.emf.observables.runtime.IObservableListObject;
import net.randomice.emf.observables.runtime.ObservableSetRootObjects;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.set.IObservableSet;
import org.eclipse.core.databinding.observable.set.ListToSetAdapter;
import org.eclipse.core.runtime.Assert;
import org.eclipse.emf.databinding.EMFObservables;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;

/**
 * An Observable list wrapper for {@link Book}
 * @generated
 */
public class BookOL implements IObservableListObject<Book> {
	public final IObservableList o;

	public BookOL(IObservableList observableList) {
		Object type = observableList.getElementType();
		if (type instanceof EReference) {
			type = ((EReference) type).getEType().getInstanceClass();
		}
		Assert.isTrue(null == type || Book.class.equals(type));
		o = observableList;
	}

	@SuppressWarnings("unchecked")
	public List<Book> getList() {
		return (List<Book>) o;
	}

	public BookOO add(Book object) {
		getObservableObjects();
		o.add(object);
		return (BookOO) observableObjects.get(object);
	}

	public BookOO addNew() {
		return add(ItemsFactory.eINSTANCE.createBook());
	}

	private IObservableSet listToSetAdapter = null;
	public IObservableSet getObservableSet() {
		if (null == listToSetAdapter) {
			listToSetAdapter = new ListToSetAdapter(o);
		}
		return listToSetAdapter;
	}

	private ObservableSetRootObjects<Book, BookOO> observableObjects = null;
	@SuppressWarnings("unchecked")
	public Set<BookOO> getObservableObjects() {
		if (null == observableObjects) {
			observableObjects = new ObservableSetRootObjects<Book, BookOO>(
					getObservableSet(), BookOO.class, BookOO.CREATOR);
		}
		return (Set<BookOO>) observableObjects;
	}

	public IObservableMap getMapFeature(EStructuralFeature feature) {
		if (ItemsPackage.Literals.ITEM__PUBLICATION_DATE.equals(feature)) {
			return getPublicationDate();
		}
		if (ItemsPackage.Literals.BOOK__TITLE.equals(feature)) {
			return getTitle();
		}
		if (ItemsPackage.Literals.BOOK__PAGES.equals(feature)) {
			return getPages();
		}
		if (ItemsPackage.Literals.BOOK__CATEGORY.equals(feature)) {
			return getCategory();
		}
		if (ItemsPackage.Literals.BOOK__AUTHOR.equals(feature)) {
			return getAuthor().o;
		}
		throw new UnsupportedOperationException("Feature " + feature
				+ " not supported!");
	}

	// features from type Item

	private IObservableMap publicationDate = null;
	public IObservableMap getPublicationDate() {
		if (null == publicationDate) {
			publicationDate = EMFObservables.observeMap(getObservableSet(),
					ItemsPackage.Literals.ITEM__PUBLICATION_DATE);
		}
		return publicationDate;
	}

	// features from type Book

	private IObservableMap title = null;
	public IObservableMap getTitle() {
		if (null == title) {
			title = EMFObservables.observeMap(getObservableSet(),
					ItemsPackage.Literals.BOOK__TITLE);
		}
		return title;
	}

	private IObservableMap pages = null;
	public IObservableMap getPages() {
		if (null == pages) {
			pages = EMFObservables.observeMap(getObservableSet(),
					ItemsPackage.Literals.BOOK__PAGES);
		}
		return pages;
	}

	private IObservableMap category = null;
	public IObservableMap getCategory() {
		if (null == category) {
			category = EMFObservables.observeMap(getObservableSet(),
					ItemsPackage.Literals.BOOK__CATEGORY);
		}
		return category;
	}

	private WriterOM<Book> author = null;
	public WriterOM<Book> getAuthor() {
		if (null == author) {
			author = new WriterOM<Book>(Book.class, EMFObservables.observeMap(
					getObservableSet(), ItemsPackage.Literals.BOOK__AUTHOR));
		}
		return author;
	}

}
